//
//  MyCarsView.swift
//  fho
//
//  Created by 郭颢源 on 2021/4/10.
//

import UIKit

//typealias void(^TouchEndSelectedTagBlock)(int tag)
typealias TouchEndSelectedTagBlock = (_ tag:Int)->Void
class MyCarsView: UIView {
    var singleLabelH:CGFloat!
    var selectTag:Int!
    var dataArray:Array<Any>! = nil
    var touchEnd: TouchEndSelectedTagBlock?
    
    func initWithDataArray(_ data:Array<Any>,singleH:CGFloat, touchEndBlock: @escaping TouchEndSelectedTagBlock) -> MyCarsView {
        assert(data.count > 0, "")
        assert(singleH > 0, "")
        
        singleLabelH = singleH
        dataArray = data
        selectTag = -1
        touchEnd  = touchEndBlock
        setupViews()
        return self
    }
    
    func setupViews(){
        for i in 0..<dataArray.count {
            let titleLabel:MyLabel = MyLabel()
            titleLabel.text = (dataArray[i] as! String)
            titleLabel.font = UIFont.boldSystemFont(ofSize: 14)
            titleLabel.textAlignment = .center
            titleLabel.textColor = UIColor(r: 123, g: 123, b: 123, a: 1)
            titleLabel.tag = i
            addSubview(titleLabel)
            titleLabel.snp.makeConstraints { (make) in
                make.height.equalTo(singleLabelH)
                make.top.equalTo(CGFloat(i) * singleLabelH)
                make.right.left.equalTo(0);
            }
        }
    }
    //触摸开始
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let beganPoint = touch?.location(in: self)
        let tag:Int = Int(beganPoint!.y/singleLabelH)
        
        
        makeBeganMove(tag, andPoint: beganPoint!)
    }
    //触摸滑动
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let touchMove = touch?.location(in: self)
        let tag: Int = Int(touchMove!.y/self.singleLabelH)
        makeMove(tag: tag, andPoint: touchMove!)
    }
    
    //触摸停止
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let touchEnd = touch?.location(in: self)
        let tag: Int = Int(touchEnd!.y/self.singleLabelH)
        makeEndMove(tag: tag, andPoint: touchEnd!)
    }
    
    //触摸开始的时候的动画
    func makeBeganMove(_ tag:Int, andPoint:CGPoint){
        for label in self.subviews {
            if label.isKind(of: MyLabel.self) {
                let label = label as! MyLabel
                //中间的那个
                if label.tag == tag {
                    makeLabelMove(label, toValue: -40, fromValue: label.fromValue)
                }
                //上边的四个或者少于四个
                if label.tag>=tag-4 && label.tag>=0 && label.tag<tag {
                    let toValue:Int = -(4-(tag-label.tag))*10
                    makeLabelMove(label, toValue: toValue, fromValue: label.fromValue)
                }
                //下边的四个或者少于四个
                if label.tag<dataArray.count && label.tag<=tag+4 && label.tag>tag {
                    makeLabelMove(label, toValue: -(4-(-tag+label.tag))*10, fromValue: label.fromValue)
                }
            }
            
        }
        
    }
    
    //触摸的时候的动画
    func makeMove(tag:Int, andPoint point:CGPoint){
        
        if tag == self.selectTag {
            return
        }
        self.selectTag = tag;
        
        //其余的
        
        for label in self.subviews {
            
            if label.isKind(of: MyLabel.self) {
                let label = label as! MyLabel
                
                
                assert(label.tag >= 0, "")
                assert(label.tag<self.dataArray.count,"")
                
                //最在上面几个 或者没有 回归原位
                if tag-4>label.tag {
                    makeLabelMove(label, toValue: 0, fromValue: label.fromValue)
                }
                //上边的四个或者少于四个
                else if label.tag>=tag-4 && label.tag<tag {
                    
                    makeLabelMove(label, toValue: -(4-(tag-label.tag))*10, fromValue: label.fromValue)
                }
                //中间的那个
                else if (label.tag==tag) {
                    
                    makeLabelMove(label, toValue:-40, fromValue:label.fromValue)
                    
                }
                //下边的
                else if label.tag<tag+4 && label.tag>tag {
                    makeLabelMove(label, toValue: -(4-(-tag+label.tag))*10, fromValue: label.fromValue)
                }
                //最下面的几个 或者没有 回归原位 条件是//(label.tag>tag+4)
                else {
                    
                    makeLabelMove(label, toValue:0, fromValue:label.fromValue)
                }
            }
        }
    }
        //触摸结束的时候的动画
        func makeEndMove(tag:Int, andPoint point:CGPoint){
            
            if self.touchEnd != nil {
                self.touchEnd!(tag)
            }
            //所有的回归原位
            for label in self.subviews{
                if label.isKind(of: MyLabel.self) {
                    let label = label as! MyLabel
                    makeLabelMove(label, toValue: 0, fromValue: label.fromValue)
                }
            }
            
            
        }
        func makeLabelMove(_ label:MyLabel, toValue:Int, fromValue:Int){
            
            let time = 0.3
            let anim:CABasicAnimation = CABasicAnimation()
            //        anim.duration = CFTimeInterval
            anim.duration = time
            anim.isRemovedOnCompletion = false
            anim.fillMode = .forwards
            anim.keyPath = "transform.translation.x"
            anim.fromValue = fromValue
            anim.toValue = toValue
            label.fromValue=toValue
            label.layer.add(anim, forKey: nil)
            
            
            //改变颜色
            let colorMultiple:Int = -toValue/10
            if colorMultiple != 4 {
                let colorAdd:Int = colorMultiple * 35 + 123
                label.textColor = UIColor(red: CGFloat(colorAdd)/255.0, green: CGFloat(colorAdd)/255.0, blue: CGFloat(colorAdd)/255.0, alpha: 1.0)
                
            }else
            {
                label.textColor = UIColor(r: 0, g: 0, b: 0, a: 1.0)//黑色
            }
            
        }
        
        /*
         // Only override draw() if you perform custom drawing.
         // An empty implementation adversely affects performance during animation.
         override func draw(_ rect: CGRect) {
         // Drawing code
         }
         */
        
    }
